package person.xjl.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import person.xjl.domain.Student;
import person.xjl.service.StudentService;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lenovo on 2020/3/17.
 */
//@RestController
    @Controller
@RequestMapping("/Student")
//默认对象名：studentController
//有一张请求映射表存放了 请求和类/方法的对应关系
public class StudentController {
    //自动注入service层的对象studentServiceImpl,IoC,Inverse of Control
    @Autowired
    private StudentService studentServiceImpl;
    //students,列表数组
    @Getter @Setter   private List<Student> students=new ArrayList<Student>();

    //list  Students显示所有学生信息
    //支持跨域请求,80端口访问8080端口，就称为跨域
  //  @CrossOrigin
    @GetMapping("list.do")
    //list方法返回的数据用于渲染响应的body
  //  @ResponseBody
    public String  list(@CookieValue("username")String username,@RequestParam(value = "page",defaultValue = "1")int page, Model model)
    {
        //测试cookie值
        System.out.println(username);
        //页面小助手,读取的页面和每页显示的记录个数
        PageHelper.startPage(page,10);
        //students对象的值来源于service，service来源于dao,dao来源于数据库
        students=studentServiceImpl.list();
        //PageInfo对象包装学生数据
        PageInfo pageInfo=new PageInfo(students);
        //传出的数据发生变化，传pageInfo对象到listStudents.jsp页面
        //model.addAttribute("students",students);
        model.addAttribute("pageInfo",pageInfo);
        //请求转向到listStudents.jsp页面，在/WEB-INF/views下
        //return students;
        return "student/listStudents";
    }
    @RequestMapping("/toAdd.do")
    public String toAdd()
    {
        //请求转向到listStudents.jsp页面，在/WEB-INF/views下
        return "student/addStudent";
    }
    //限定请求
    @RequestMapping(value = "/add.do",method=RequestMethod.POST)
    //如果jsp传过来的body里有元素名字和方法变量名一致的，Spring MVC框架自动设置变量值
    //如果元素名和变量名不一致，就是用@RequestParam
    public String add(@RequestParam(value = "file") MultipartFile file, Student student, Model model) throws IOException {
        //实现上传
        uploadFile(file);
        student.setImage(file.getOriginalFilename());
        //获取用户传过来的数据,打印在控制台上
       // System.out.println(student.getSname()+":"+student.getPassword()+","+student.getAge());
        //调用service的add方法，实现增加学生
         studentServiceImpl.add(student);
        //请求转向list.do，即list方法,显示所有学生的信息
        return "redirect:list.do";
    }
    //uploadFile
    private void uploadFile(MultipartFile image) throws IOException {
        if(image!=null){
            File imageFile = new File(image.getOriginalFilename());// 上传后的文件保存目录及名字
            image.transferTo(imageFile);// 将上传文件保存到相应位置
        }
    }
    //delete student
    @RequestMapping("delete.do")
    public String delete(int id){
        studentServiceImpl.delete(id);
        return "redirect:list.do";
    }
//准备修改学生,找到需要修改的学生，显示其信息
    @RequestMapping("toUpdate.do")
    public String toUpdate(int id,Model model){
        Student student=studentServiceImpl.select(id);
        model.addAttribute("student",student);
        return "student/updateStudent";
    }
    //修改学生信息
    @RequestMapping("update.do")
    public String update(Student newStudent){
        studentServiceImpl.update(newStudent);
        return "redirect:list.do";
    }
//学生登录
    @RequestMapping("login.do")
    public String login(String name,String password){
        System.out.println(studentServiceImpl.login(name, password));
        if(studentServiceImpl.login(name, password)==null)
        //登录失败
        {
            //返回一个错误message
            //待在login.jsp页面上
            return "login";
        }
        else
            //登录成功
        {
            //session变量保存学生信息
            //转到listStudent.jsp页面
            return "redirect:list.do";
        }
    }
}
